/*
                 \\|//
                 (o o)
--------------ooO-(_)-Ooo-------------------------------------------------------
 
   (C)  Copyright 2004 By Gregory A. Prentice - All Rights Reserved.
   http://www.cafechess.org
  
   Redistribution and use in source and binary forms, with or without 
   modification, are permitted provided that the following conditions are met:
   
    - Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
   
    - Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
   
    - If you decide to use the code in a publicly released product then please
      give the author credit for his work.
  
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY,WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   
                      Ooo.
--------------.ooO----(  )------------------------------------------------------
              (  )    (_/
               \_)
*/
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Text;

using Cafechess.Text.Commands;
using Cafechess.Chess.Controls;
using Cafechess.Chess.Parsers;
using Cafechess.Chess.General;
using Cafechess.Chess.Validation;
using Cafechess.Chess.Engines;
using Cafechess.Chess.Engines.WBD;
using Cafechess.Chess.Engines.UCI;

namespace Cafechess.Control.Chessboard.Demo
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public partial class ChessDisplay : IValidationEvents, IChessEngineEvents
  {

    PGNtoRTF loadPGN;

    public ChessDisplay()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      
      lstCompass.Items.Add(CompassHeading.None);
      lstCompass.Items.Add(CompassHeading.North);
      lstCompass.Items.Add(CompassHeading.South);
      lstCompass.Items.Add(CompassHeading.East);
      lstCompass.Items.Add(CompassHeading.West);
      lstCompass.Items.Add(CompassHeading.NorthEast);
      lstCompass.Items.Add(CompassHeading.NorthWest);
      lstCompass.Items.Add(CompassHeading.SouthEast);
      lstCompass.Items.Add(CompassHeading.SouthWest);      
      lstCompass.SelectedIndex = 0;
      lstPieces.Items.Add(Pieces.None);
      lstPieces.Items.Add(Pieces.All);
      lstPieces.Items.Add(Pieces.AllPawns);
      lstPieces.Items.Add(Pieces.AllNonPawns);
      lstPieces.Items.Add(Pieces.AllKings);
      lstPieces.Items.Add(Pieces.AllQueens);
      lstPieces.Items.Add(Pieces.AllRooks);
      lstPieces.Items.Add(Pieces.AllBishops);
      lstPieces.Items.Add(Pieces.AllKnights);
      lstPieces.Items.Add(Pieces.AllMinors);
      lstPieces.Items.Add(Pieces.BlackKing);
      lstPieces.Items.Add(Pieces.BlackQueen);
      lstPieces.Items.Add(Pieces.BlackRook);
      lstPieces.Items.Add(Pieces.BlackBishop);
      lstPieces.Items.Add(Pieces.BlackKnight);
      lstPieces.Items.Add(Pieces.BlackPawn);
      lstPieces.Items.Add(Pieces.WhiteKing);
      lstPieces.Items.Add(Pieces.WhiteQueen);
      lstPieces.Items.Add(Pieces.WhiteRook);
      lstPieces.Items.Add(Pieces.WhiteBishop);
      lstPieces.Items.Add(Pieces.WhiteKnight);
      lstPieces.Items.Add(Pieces.WhitePawn);


      lstPieces.SelectedIndex = 0;

      EngineCommands.SelectedIndex = 0;
      NewChessboard.ChessBoardGui.Validation = new StandardValidation();
      NewChessboard.ChessBoardGui.Validation.AddEvents(this);      
      IChessboardGui igui = NewChessboard.ChessBoardGui;
      NewChessboard.EnableAltKeys = true;
      
      igui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawTop;
      igui.AttacksColor = Color.PowderBlue;
      igui.DefaultDrawingColor = Color.OrangeRed;
      igui.IsDrawing = true;
      NewChessboard.ChessBoardGui.FenNotation = "rnbqkb1r/pppppppp/5n2/8/2P5/8/PP1PPPPP/RNBQKBNR b KQkq - 0 2";
      moveColor.BackColor = NewChessboard.ChessBoardGui.Validation.SideToMove ? System.Drawing.Color.White : System.Drawing.Color.Black;
    }



    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      ChessDisplay aDisp = new ChessDisplay();      
      try
      {
        Application.Run(aDisp);
      }
      finally
      {      
        aDisp.Dispose(true);
      }
    }

    private void dragCHK_CheckedChanged(object sender, System.EventArgs e)
    {
    }

    private void switchBTN_Click(object sender, System.EventArgs e)
    {
      NewChessboard.ChessBoardGui.IsFlipped = !NewChessboard.ChessBoardGui.IsFlipped;
    }

                          
    private void printBTN_Click(object sender, System.EventArgs e)
    {
      gameNotation.Clear();
      gameNotation.Text = NewChessboard.ChessBoardGui.Validation.ToString();
    }

    private void btnNew_Click(object sender, System.EventArgs e)
    {
      statusLBL.Text = "";
      moveColor.BackColor = System.Drawing.Color.White;
      //coMoveNumber=1;
      NewChessboard.ChessBoardGui.NewPosition();
    }
    private void fenBTN_Click(object sender, System.EventArgs e)
    {
      NewChessboard.ChessBoardGui.FenNotation = fenTXT.Text;
    }

    private void getFenBtn_Click(object sender, System.EventArgs e)
    {
      fenTXT.Text = NewChessboard.ChessBoardGui.FenNotation;
    }
    #region IValidationEvents Members

    public void KingIsStaleMated()
    {
      statusLBL.Text = "STALE MATE!";    
    }

    public void KingIsInCheck()
    {
      statusLBL.Text = "CHECK!";
    }

    public void KingIsMated()
    {
      statusLBL.Text = "MATE!";    
    }

    public void KingIsFree()
    {
      statusLBL.Text = "";    
    }

    public void BeforeMove(Pieces piece, string from, string to)
    {
    }
    public void AfterMove(Pieces piece, string from, string to)
    {
// Some long delay to test gui updating.
//      long count;
//      for(long cnt=0;cnt < 5000000; cnt++)
//        count = cnt * 5;
      if(NewChessboard.ChessBoardGui.Validation.IsValidMove )
      {
        moveLBL.Text = NewChessboard.ChessBoardGui.Validation.FullNotation;
        moveColor.BackColor = NewChessboard.ChessBoardGui.Validation.SideToMove ? System.Drawing.Color.White : System.Drawing.Color.Black;        
      }
    }
    public void Promotion(bool color, string square, ref Pieces piece)
    {
      if(color)
        piece = Pieces.WhiteQueen;
      else
        piece = Pieces.BlackQueen;
    }

    #endregion

    private void fenTXT_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    }

    private void validChk_CheckedChanged(object sender, System.EventArgs e)
    {
//      chessBoard1.isValidating = validChk.Checked;    
    }

    private void gameNavigation_Load(object sender, System.EventArgs e)
    {
      //gameNavigation.SetDataBinding( , );
    }

    private void pgnBtn_Click(object sender, System.EventArgs e)
    {
      System.Windows.Forms.OpenFileDialog oFileDialog = new System.Windows.Forms.OpenFileDialog();      
      if( System.Windows.Forms.DialogResult.OK == oFileDialog.ShowDialog() )
      {        
        PGNtoXML gameEvents = new PGNtoXML();
        try
        {
          string strXMLFile = System.IO.Path.GetDirectoryName(oFileDialog.FileName) + "\\" + System.IO.Path.GetFileNameWithoutExtension( oFileDialog.FileName ) + ".xml";
          gameEvents.open(strXMLFile);
          PGNparser pgn = new PGNparser();
          pgn.Filename =  oFileDialog.FileName;          
          pgn.AddEvents(gameEvents);
          pgn.Parse();
        }       
        catch
        {
        }
        finally
        {
          gameEvents.close();
        }
      }
    }
    
    private void LoadBTN_Click(object sender, System.EventArgs e)
    {
      System.Windows.Forms.OpenFileDialog oFileDialog = new System.Windows.Forms.OpenFileDialog();       
      if( System.Windows.Forms.DialogResult.OK == oFileDialog.ShowDialog() )
      {
        try
        {
          if(loadPGN == null)
            loadPGN = new PGNtoRTF(gameNotation);
          gameNotation.Text = "";
          PGNparser pgn = new PGNparser();
          pgn.Filename =  oFileDialog.FileName;
          pgn.AddEvents(loadPGN);
          pgn.Parse();
        }       
        catch
        {
        }
      }
    }

    private void chkEditPosition_CheckedChanged(object sender, System.EventArgs e)
    {
      NewChessboard.ChessBoardGui.IsEditting = chkEditPosition.Checked;
      NewChessboard.ChessBoardGui.IsDrawing = chkEditPosition.Checked;
    }

    private void gameNavigation_Load_1(object sender, System.EventArgs e)
    {
    
    }
    
    bool toggle=true;
    private void btnHighlight_Click(object sender, System.EventArgs e)
    {
      try
      {
        IChessboardGui igui = NewChessboard.ChessBoardGui;
        NewChessboard.SuspendLayout();
        igui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawMid;
        igui.Highlight(toggle? Color.Green:Color.Transparent,4,"f6");
        igui.Highlight(toggle? Color.Crimson:Color.Transparent,4,"a2");
        NewChessboard.ResumeLayout();
        NewChessboard.Invalidate();
        toggle = !toggle;      
      }
      catch(Exception ex)
      {
        ex.ToString();
      }    
    }

    private void testTime_ValueChanged(object sender, System.EventArgs e)
    {
      
    }

    private void squareTest_CheckedChanged(object sender, System.EventArgs e)
    {
//      if(squareTest.Checked)
//        chessBoard1.testSquares( (int)testTime.Value );
//      else
//        chessBoard1.testSquares( 0 );
    }

    private void btnSquareColors_Click(object sender, System.EventArgs e)
    {
      NewChessboard.ChessBoardGui.ColorSquares(Color.LightBlue, Color.DarkBlue);
      NewChessboard.ChessBoardGui.ColorBoarder(Color.DarkGoldenrod);
      NewChessboard.Invalidate();   
    }

    private void EngineCommands_SelectedValueChanged(object sender, System.EventArgs e)
    {
    }
    Xboard xboardEngine;
    UCIboard uciEngine;
    CraftyAnalysis analysisInfo;
    ChessEngines engineConfig;

    private void btnSubmitCommand_Click(object sender, System.EventArgs e)
    {
      if (engineConfig == null)
      {        
        //Cafechess.Chess.Engines.
        ChessEngines allEngines = Factory.LoadConfig(@"C:\Source\NET\LIB\Cafechess\src\Cafechess.Chess\Engines\ChessEngines.xml");

        engineConfig = Cafechess.Chess.Engines.Factory.LoadConfig(@"C:\Source\NET\LIB\Cafechess\src\Cafechess.Chess\Engines\ChessEngines.xml");        
      }
      switch( EngineCommands.SelectedIndex )
      {
        case 0:

          if (checkUCI.Checked == false)
          {
            if (xboardEngine != null)
            {
              xboardEngine.Dispose();
              xboardEngine = null;
              analysisInfo = null;
              updateAnalysis.Enabled = false;
            }
          }
          else
          {
            if (uciEngine != null)
            {
              uciEngine.Dispose();
              uciEngine = null;
              analysisInfo = null;
              updateAnalysis.Enabled = false;
            }
          }
          break;
        case 1:
          gameNotation.Clear();
          if (checkUCI.Checked == false)
          {

            if (xboardEngine == null)
            {
                xboardEngine = new Xboard(this, engineConfig.ChessEngineInterface[0]);
              analysisInfo = new CraftyAnalysis(bbText);
              analysisInfo.SideToMove = NewChessboard.ChessBoardGui.Validation.SideToMove;
              xboardEngine.Start();
            }
          }
          else
          {
            if (uciEngine == null)
            {
                uciEngine = new UCIboard(this, engineConfig.ChessEngineInterface[1]);
              uciEngine.Start();
            }
          }
          break;
        case 2:
          if (checkUCI.Checked == false)
          {
            if (xboardEngine != null)
            {
              bbText.Clear();
              analysisInfo.Clear();
              xboardEngine.Analyze(true);
              analysisInfo.SideToMove = NewChessboard.ChessBoardGui.Validation.SideToMove;
              xboardEngine.SetBoard(NewChessboard.ChessBoardGui.Validation.FenNotation);
              updateAnalysis.Enabled = true;
            }
          }
          else
          {
            if (uciEngine != null)
            {
              bbText.Clear();
              uciEngine.Analyze(true);
              uciEngine.SetBoard(NewChessboard.ChessBoardGui.Validation.FenNotation);

            }
          }
          break;
        case 3:
          if (checkUCI.Checked == false)
          {

            if (xboardEngine != null)
              xboardEngine.Analyze(false);

          }
          else
          {
            if (uciEngine != null)
              uciEngine.Analyze(false);
          }
          updateAnalysis.Enabled=false;
          break;
        case 4:
          if (checkUCI.Checked == false)
          {

            if (xboardEngine != null)
            {
              analysisInfo.Clear();
              analysisInfo.SideToMove = NewChessboard.ChessBoardGui.Validation.SideToMove;
              xboardEngine.SetBoard(NewChessboard.ChessBoardGui.Validation.FenNotation);
            }
          }
          else
          {
            if (uciEngine != null)
            {
              xboardEngine.SetBoard(NewChessboard.ChessBoardGui.Validation.FenNotation);
            }
          }
          break;
      }   
    }

    bool coToggle=true;    
    private void btnAttacks_Click(object sender, System.EventArgs e)
    {
      IChessboardGui igui = NewChessboard.ChessBoardGui;
      BitArray ba = igui.Validation.Attacks(igui.Validation.SideToMove);
      Color acolor = coToggle?Color.LightYellow:Color.Transparent;
      igui.AlternateColorSquares(ba,acolor);
      coToggle = !coToggle;
      NewChessboard.Invalidate();
    }

    private void btnDefender_Click(object sender, System.EventArgs e)
    {
      IChessboardGui igui = NewChessboard.ChessBoardGui;
      BitArray ba = igui.Validation.Attacks(!igui.Validation.SideToMove);
      Color acolor = coToggle?Color.LightYellow:Color.Transparent;
      igui.AlternateColorSquares(ba,acolor);
      coToggle = !coToggle;
      NewChessboard.Invalidate();
    }
    #region IChessEngineEvents Members

    public void Results(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.WhiteWins implementation
    }

    public void TellUser(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellUser implementation
    }
    public void AskUser(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.AskUser implementation
    }

    public void TellUserError(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellUserError implementation
    }

    public void MakeMove(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.MakeMove implementation
    }

    public void Information(object sender, System.EventArgs e)
    {
      ChessEventArgs args = (ChessEventArgs)e;
      //gameNotation.AppendText(information);
    }

    public void TellIcs(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellIcs implementation
    }

    public void TellIcsNoAlias(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellIcsNoAlias implementation
    }

    public void OfferDraw(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.OfferDraw implementation
    }

    public void IllegalMove(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.IllegalMove implementation
    }

    public void TellAll(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellAll implementation
    }

    public void Analysis(object sender, System.EventArgs e)
    {
      analysisInfo.Add(((ChessEventArgs)e).Information);
      analysisInfo.DisplayAnalysis();
    }    
    public void Error(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.Error implementation
    }

    public void TellOthers(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellOthers implementation
    }

    public void TellOpponent(object sender, System.EventArgs e)
    {
      // TODO:  Add ChessDisplay.TellOpponent implementation
    }

    public void Draw()
    {
      // TODO:  Add ChessDisplay.Draw implementation
    }

    #endregion

//    bool coToggleWeak=true;
    private void btnWhiteControl_Click(object sender, System.EventArgs e)
    {
//      if(coToggleWeak)
//      {
//        SquareImageList hi = new SquareImageList(Color.LightSkyBlue, new Size(100,100), Color.Black);
//        chessBoard1.showWeakSquares(true,hi.SquareImages);
//      }
//      else
//      {
//        chessBoard1.showAttacks(false,null);
//      }
//      coToggleWeak = !coToggleWeak;
      NewChessboard.ChessBoardGui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawTop;
      NewChessboard.ChessBoardGui.ClearLayer();
    }

    private void btnTools_Click(object sender, System.EventArgs e)
    {
      cafechess.chessboard.demo.EditBoard eb = new cafechess.chessboard.demo.EditBoard();
      eb.FenNotation = NewChessboard.ChessBoardGui.FenNotation;
      if( eb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
        NewChessboard.ChessBoardGui.FenNotation = eb.FenNotation;
        moveColor.BackColor = NewChessboard.ChessBoardGui.Validation.SideToMove ? System.Drawing.Color.White : System.Drawing.Color.Black;
      }
    }

    private void btnDrawArrows_Click(object sender, System.EventArgs e)
    {
      try
      {
        IChessboardGui igui = NewChessboard.ChessBoardGui;
        NewChessboard.SuspendLayout();
        igui.ActiveLayer = Cafechess.Chess.Controls.Layers.Surface;
        //igui.ActiveLayer = cafechess.control.chessboard.Layers.DrawTop;
        Rectangle rt = igui.NotationSquare("e5");
        igui.Balloon("Hello there Mr Bill how is Gumby doing?",toggle?10:-1,new Rectangle(rt.X + 5,rt.Y + 5,120,100));
        igui.ActiveLayer =Cafechess.Chess.Controls.Layers.DrawTop;
        igui.Arrow(toggle?Color.Red:Color.Transparent,4,"a1", "d4");
        igui.Xmarks(toggle?Color.Red:Color.Transparent,4,"d8");
        igui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawMid;
        igui.Circle(toggle?Color.Orange:Color.Transparent,4,"b2");
        igui.Arrow(toggle?Color.Purple:Color.Transparent,4,"d8", "d4");
        NewChessboard.ResumeLayout();
        NewChessboard.Invalidate();
        toggle = !toggle;      
      }
      catch(Exception ex)
      {
        ex.ToString();
      }        
    }

    private void ChessDisplay_Load(object sender, System.EventArgs e)
    {
      Displayhelp();
    }

    private void btnDrawFormat_Click(object sender, System.EventArgs e)
    {
      IChessboardGui igui = NewChessboard.ChessBoardGui;
      igui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawTop;
      fenTXT.Text = igui.LayerNotation;
      igui.ClearLayer();
      NewChessboard.Invalidate();
    }

    private void btnGetDrawFen_Click(object sender, System.EventArgs e)
    {
      IChessboardGui igui = NewChessboard.ChessBoardGui;          
      igui.ActiveLayer = Cafechess.Chess.Controls.Layers.DrawTop;      
      igui.LayerNotation = fenTXT.Text;
      NewChessboard.Invalidate();
    }

    private void btnSaveImg_Click(object sender, System.EventArgs e)
    {
      System.Windows.Forms.SaveFileDialog oFileDialog = new System.Windows.Forms.SaveFileDialog();      
      if( System.Windows.Forms.DialogResult.OK == oFileDialog.ShowDialog() )
      {        
        IChessboardGui igui = ChessImageFactory.CreateIChessboardGui(new Rectangle(0,0,200,200),Color.Gray);
        igui.ChessImages = imagePieces;
        igui.BoarderWidth = 15;
        igui.FenNotation = fenTXT.Text;
        igui.ActiveLayer = Layers.DrawTop;
        igui.LayerNotation = "a:a1:d4:Red:4/x:d8:Red:4";
        igui.ActiveLayer = Layers.DrawMid;
        igui.LayerNotation = "c:e4:Blue:2/c:e5:Orange:4";
        Bitmap bm = igui.ChessImage();
        bm.Save(oFileDialog.FileName);    
      }
    }

    private void lstPieces_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      if( lstPieces.SelectedItem != null && lstCompass.SelectedItem != null )
        NewChessboard.ChessBoardGui.Hide((Pieces)lstPieces.SelectedItem, 
          (CompassHeading)lstCompass.SelectedItem);

    }

    private void lstCompass_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      if( lstPieces.SelectedItem != null && lstCompass.SelectedItem != null )
        NewChessboard.ChessBoardGui.Hide((Pieces)lstPieces.SelectedItem, 
                                         (CompassHeading)lstCompass.SelectedItem);
    }

    private void chkHideDragging_CheckedChanged(object sender, System.EventArgs e)
    {
      NewChessboard.ChessBoardGui.IsShowPieceDragging = !chkHideDragging.Checked;
    }

    private void btnPromo_Click(object sender, System.EventArgs e)
    {
      IChessboardGui igui = NewChessboard.ChessBoardGui;
      igui.FenNotation = "8/2kPK3/8/8/8/8/8/8 w - - 0 2";
      
      igui.Validation.Move("d8=Q+");   
    }

    private void Displayhelp()
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendLine("Press the following Keys:");
      sb.AppendLine("\t'Alt + F9' turns on major piece square highlights when a major piece is dropped on a square.");
      sb.AppendLine("\t'Alt + F10' turns on attack square highlighting and locks it for the current side to move.");
      sb.AppendLine("\t'Alt + F11' turns on attack square highlighting for the side not to move.");
      sb.AppendLine("\t'Alt + F12' turns on square control highlighting for the side not to move.");
      sb.AppendLine("\t'Ctrl + a' click left mouse button on start sqare drag to destination square release button and an arrow is drawn on top layer.");
      sb.AppendLine("\t'Ctrl + c' click left mouse button on a square and a circle is drawn on top layer.");
      sb.AppendLine("\t'Ctrl + x' click left mouse button on a square and a X is drawn on top layer.");
      sb.AppendLine("\t'Ctrl + Alt + ' any a,c,x produces that mark on the middle layer (between board and pieces)");
      this.bbText.Text = sb.ToString();
    }

    private void btnWeak_Click(object sender, EventArgs e)
    {

    }

    private void btnHuffman_Click(object sender, EventArgs e)
    {
        HuffmanNotation huffOriginal = new HuffmanNotation();
        FenNotation fenOriginal = new FenNotation();

        string notationOriginal = this.NewChessboard.ChessBoardGui.FenNotation;

        fenOriginal.AddEvents(huffOriginal);
        fenOriginal.Parse(notationOriginal);

        HuffmanNotation huffNew = new HuffmanNotation();
        FenNotation fenNew = new FenNotation();
        huffNew.AddEvents(fenNew);
        huffNew.Blob = huffOriginal.Blob;
        string notationNew = fenNew.ToString();
        this.bbText.Text = string.Format("Result:\t\t{0}\r\nOriginal FEN:\t{1}\r\nHuffman FEN:\t{2}\r\n", 
            notationOriginal == notationNew ? "Success" : "Failed",
            notationOriginal, notationNew);
             

    }

  }
}
